import 'package:app/widget/home_bottom.dart';
import 'package:app/widget/l_radius_button.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  int _index = 0;
  final icons = [
    "assets/images/icon_tab_bottom_nor2.png",
    "assets/images/icon_tab_bottom_nor3.png",
    "assets/images/icon_tab_bottom_nor4.png"
  ];
  final tabArray = ["首页", "书架", "我的"];
  final selectedIcons = [
    "assets/images/icon_tab_bottom_pre2.png",
    "assets/images/icon_tab_bottom_pre3.png",
    "assets/images/icon_tab_bottom_pre4.png"
  ];

  final textSelectedColor = Color(0xff31af00);
  var text;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          children: [
            Expanded(
              child: IndexedStack(
                index: _index,
                children: [
                  Container(
                    width: double.infinity,
                    color: Colors.red,
                    child: Column(
                      children: [
                        LRadiusButton(
                          text: "确定",
                          color: Colors.blue,
                          width: 120.0,
                          height: 80.0,
                          borderRadius: BorderRadius.circular(15),
                          padding: EdgeInsets.all(10),
                          onTap: onTap,
                        ),
                        LRadiusButton(
                          text: text ?? "",
                          color: Colors.blue,
                          borderRadius: BorderRadius.circular(15),
                          padding: EdgeInsets.all(10),
                          onTap: onGet,
                        ),
                      ],
                    ),
                  ),
                  Container(
                    color: Colors.yellow,
                  ),
                  Container(
                    color: Colors.blue,
                  )
                ],
              ),
            ),
            HomeBottom(
              currentItem: _index,
              height: 60.0,
              length: tabArray.length,
              icons: icons,
              selectedIcons: selectedIcons,
              textSelectedColor: textSelectedColor,
              array: tabArray,
              onItemSelectedCallback: (index) {
                setState(() {
                  _index = index;
                });
              },
            )
          ],
        ),
      ),
    );
  }

  Future<void> onTap() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString("tag", "您好");
  }

  Future<void> onGet() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    var s = sharedPreferences.get("tag");
    print(s);
    setState(() {
      text = s;
    });
  }
}
